package longbottom.XLkprob;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.BarChart.Type;
import org.achartengine.model.CategorySeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;

public class MyGraphFactory {

	public static int max(int a, int b) {
		return (a > b) ? a : b;
	}

	public static int min(int a, int b) {
		return (a < b) ? a : b;
	}

	public static GraphicalView makeDoubleChart(Context context,
			CategorySeries series1, CategorySeries series2) {

		int maxX = min(max(series1.getItemCount(), series2.getItemCount()) - 1,
				10);

		XYMultipleSeriesDataset dataSet = new XYMultipleSeriesDataset();
		dataSet.addSeries(series1.toXYSeries());
		dataSet.addSeries(series2.toXYSeries());

		// customisation of the chart
		XYSeriesRenderer renderer1 = new XYSeriesRenderer();
		renderer1.setColor(Color.RED);
		renderer1.setDisplayChartValues(true);
		renderer1.setLineWidth((float) 5d);

		XYSeriesRenderer renderer2 = new XYSeriesRenderer();
		renderer2.setColor(Color.BLUE);
		renderer2.setDisplayChartValues(true);
		renderer2.setLineWidth((float) 5d);

		XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
		mRenderer.addSeriesRenderer(renderer1);
		mRenderer.addSeriesRenderer(renderer2);

		renderer1.setChartValuesTextSize((float) 18.0);
		renderer2.setChartValuesTextSize((float) 18.0);

		mRenderer.setXTitle((String) context.getResources().getText(
				R.string.casulties));
		mRenderer.setYTitle((String) context.getResources().getText(
				R.string.probability));
		mRenderer.setZoomButtonsVisible(false);
		mRenderer.setPanEnabled(true, true);
		mRenderer.setShowLegend(true);
		mRenderer.setFitLegend(true);
		mRenderer.setShowGridY(true);
		mRenderer.setBarSpacing(.1);
		mRenderer.setApplyBackgroundColor(true);
		mRenderer.setBackgroundColor(Color.BLACK);
		// Have to add 0.5 as the bars actually appear on either side of the
		// label
		mRenderer.setXAxisMin(0.5);
		mRenderer.setXAxisMax(maxX + 1 + 0.5);
		mRenderer.setYAxisMin(0);
		mRenderer.setYAxisMax(105);

		mRenderer.setXLabels(0);
		for (int i = 0; i <= max(series1.getItemCount(), series2.getItemCount()); i++) {
			mRenderer.addXTextLabel(i + 1, "" + i);
		}

		return ChartFactory.getBarChartView(context, dataSet, mRenderer,
				Type.DEFAULT);
	}

}
